package com.lynn.springboot_demo.utils;


import com.lynn.springboot_demo.model.Person;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.request.QueryRequest;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.params.SolrParams;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author cl
 * @Date 2020/5/18 17:15
 * solr工具类
 */
public class SolrUtil {

    //使用的集合的名称
    private static final String CORE_NAME = "cl_core";

    /**
     * 获取客户端的连接
     *
     * @return
     */
    public HttpSolrClient createSolrServer() {
        System.out.println(PropertiesUtil.getPropertiesValue("solr.url"));
        HttpSolrClient solr = null;
        solr = new HttpSolrClient.Builder(PropertiesUtil.getPropertiesValue("solr.url")).build();
        System.out.println("solr连接：" + solr);
        return solr;
    }

    /**
     * 通过java Bean 添加或者更新索引
     * id存在进行更新 不存在进行添加
     *
     * @throws IOException
     * @throws SolrServerException
     */
    public void addDocByBean() throws IOException, SolrServerException {
        //创建solr服务对象，通过此对象向solr服务发起请求
        HttpSolrClient client = createSolrServer();
        Person person = new Person("087877", "老张", "逗比程序员", 31);
        //bean添加索引
        client.addBean(CORE_NAME, person);
        //提交
        client.commit(CORE_NAME);
        System.out.println("添加成功");
    }

    /**
     * 通过域field添加索引
     *
     * @throws IOException
     * @throws SolrServerException
     */
    public void addDocByFields() throws IOException, SolrServerException {
        HttpSolrClient client = createSolrServer();
        //创建document
        SolrInputDocument document = new SolrInputDocument();
        //向文件中添加域,这些域要在solr文件的集合中schema.xml中配置，不然无效
        document.addField("id", "878787");
        document.addField("name", "小苟");
        document.addField("desc", "肥头大耳的程序员");
        document.addField("age", 35);
        //在指定集合下创建索引
        client.add(CORE_NAME, document);
        //提交
        client.commit(CORE_NAME);
        System.out.println("添加成功!");

    }

    /**
     * 根据id从索引库删除索引
     *
     * @throws IOException
     * @throws SolrServerException
     */
    public void deleteDocById(String id) throws IOException, SolrServerException {
        HttpSolrClient solr = createSolrServer();
        solr.deleteById(CORE_NAME, id);
        solr.commit(CORE_NAME);
        System.out.println("删除成功!");
    }

    /**
     * 查询
     *
     * @param name
     * @return
     * @throws IOException
     * @throws SolrServerException
     */
    public List querySolr(String name, int[] fq) throws IOException, SolrServerException {
        HttpSolrClient client = createSolrServer();
        SolrQuery query = new SolrQuery();
        if (name == null || "".equals(name)) {
            //查询全部
            query.setQuery("*:*");
        }else {
            //查询关键字，q不能省略
            query.set("q", "name:" + name);
        }
        if (fq != null) {
            //指定过滤
            query.set("fq", "age:[" + fq[0] + " TO " + fq[1] + "]");
        }
        //排序
        //参数:field域,排序类型(asc:从小到大;desc:从大到小)
        query.addSort("age", SolrQuery.ORDER.desc);
        //执行查询,指定查询的集合名称:CORE_NAME
        QueryResponse response = client.query(CORE_NAME, query);
        //从响应中得到结果
        SolrDocumentList results = response.getResults();
        System.out.println("results:" + results);
        long numFound = results.getNumFound();
        if (numFound <= 0) {
            return null;
        }
        List<Map<String, Object>> list = new ArrayList<>();
        for (SolrDocument doc : results) {
            Map<String, Object> map = doc.getFieldValueMap();
            list.add(map);

        }
        return list;
    }

    public static void main(String[] args) throws IOException, SolrServerException {
        SolrUtil solr = new SolrUtil();
//        java对象添加到文档document
//        solr.addDocByBean();

//        通过field域添加文档document
//        solr.addDocByFields();
//        删除id为7的文档
//        solr.deleteDocById("7");
        //添加过滤查询的条件，年龄20到30的
        int[] fq = {20,30};
        List list = solr.querySolr("",fq);
        System.out.println(list);
    }
}
